capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B17
*/

local data "" ;
local output "" ;


use "prworaexact_bw264_crime_poly1allcov_iv_all.dta", clear ;
keep variables n_age18unfav sen_age18unfav mean_rd ;
rename n_age18unfav b_endo ;
rename sen_age18unfav se_endo ;

gen trt_level = b_endo + mean_rd ;
gen hi95_endo = trt_level + 1.96*se_endo ;
gen lo95_endo = trt_level - 1.96*se_endo ;

gen type = "" ;
gen num = . ;
gen thresh = "" ;

keep if substr(variables,1,2) == "ct" ;

foreach type in char genr { ;	
	foreach thresh in exact gt { ;
	
		forval num = 1(1)20 { ;
			replace type = "`type'" 		if variables == "ct_`thresh'_crime2`type'`num'" ;
			replace num  = `num'    		if variables == "ct_`thresh'_crime2`type'`num'" ;
			replace thresh = "`thresh'" 	if variables == "ct_`thresh'_crime2`type'`num'" ;
		} ;
	} ;
} ;

sort type thresh num ;
gen numXcntrl 	= num * mean_rd 	if thresh == "exact" ;
gen numXtreat   = num * trt_level 	if thresh == "exact" ;
gen numXeffect 	= num * b_endo  	if thresh == "exact" ;

foreach y in cntrl treat effect { ;
	egen totX`y' = sum(numX`y'), by(type thresh) ;
} ;

graph set window fontface "Helvetica";

twoway 
	(bar trt_level mean_rd num 	if type == "genr" & thresh == "exact" & num <= 10 , color(blue blue*.5)) 
	(rcap hi95 lo95 num 		if type == "genr" & thresh == "exact" & num <= 10 , color(blue*.25)) 
	, 
		title("Effects on number of charges (exact)", size(medsmall))
		legend(off)
		xsc(r(1 10)) xlab(1(1)10) xtitle("Number of income-generating charges") 
		ytitle("Mean + IV estimate", size(medsmall))
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		yline(0,lcolor(gray))
		;
		graph export "`output'/figB17_a.png", width(1000) height(750) replace ;
	
twoway 
	(bar trt_level mean_rd num 	if type == "genr" & thresh == "gt" & num <= 10 , color(blue blue*.5)) 
	(rcap hi95 lo95 num 		if type == "genr" & thresh == "gt" & num <= 10 , color(blue*.25)) 
	, 
		title("Effects on number of charges (at least)", size(medsmall))
		legend(off)
		xsc(r(1 10)) xlab(1(1)10) xtitle("Number of income-generating charges (>=)") 
		ytitle("Mean + IV estimate", size(medsmall))
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		yline(0,lcolor(gray))
		;
		graph export "`output'/figB17_b.png", width(1000) height(750) replace ;


